// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// n [author] rhyme_lph
// S [email]  rhymelph@gmail.com
// t [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/employee_contact_entity.dart';
import 'package:finance_app/src/page/hr_payroll/employee/views/employee_other_view.dart';
import 'package:finance_app/src/page/hr_payroll/employee/views/employee_view.dart';
import 'package:finance_app/src/page/hr_payroll/employee/views/particulars_view.dart';
import 'package:finance_app/src/utils/permission_utils.dart';
import 'package:finance_app/src/widgets/button/normal_popup_button.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/common_tab_bar.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/hr_payroll/employee/new_employee_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:tuple/tuple.dart';

export 'package:finance_app/src/entities/employee_contact_entity.dart';
export 'package:finance_app/src/provider/hr_payroll/employee/new_employee_provider.dart';

@RouterMeta(paramName: "newEmployeePage")
class NewEmployeePage extends StatefulWidget {
  final EmployeeContactEntity data;
  final bool isDuplicate;
  final int id;

  const NewEmployeePage({Key key, this.data, this.isDuplicate, this.id})
      : super(key: key);

  @override
  _NewEmployeePageState createState() => _NewEmployeePageState();
}

class _NewEmployeePageState extends State<NewEmployeePage>
    with
        PagePresenter<NewEmployeePage, NewEmployeeProvider>,
        TickerProviderStateMixin {
  TabController tabController;

  String get title =>
      '${provider.id == null ? S.current.new_employee : provider.isEdit == true ? S.current.edit_employee : S.current.employee}';

  Widget get rightButton => listBuilder<bool>(
        selector: (_, __) => __.isEdit,
        builder: (BuildContext context, bool value, Widget child) =>
            provider.id != null && provider.isEdit == false
                ? _buildPopButton()
                : _buildSaveBtn(),
      );

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
          titleWidget: listBuilder<bool>(
            selector: (_, __) => __.isEdit,
            builder: (BuildContext context, bool value, Widget child) =>
                Text(title),
          ),
          actions: [
            if (Config.hasPermission(PermissionUtils.hrPayroll)) rightButton,
          ]);

  Widget _buildPopButton() => listBuilder<Tuple2<bool, int>>(
        selector: (_, __) => Tuple2(__.isEdit, __.menuList.length),
        builder:
            (BuildContext context, Tuple2<bool, int> value, Widget child) =>
                value.item1 == true || value.item2 == 0
                    ? Container()
                    : NormalMorePopupButton(
                        onSelect: provider.onSelectMenu,
                        menuList: provider.menuList,
                      ),
      );

  Widget _buildSaveBtn() {
    return listBuilder<bool>(
      selector: (_, __) => pageStatus != PageStatus.loading,
      builder: (BuildContext context, bool value, Widget child) => value == true
          ? NormalSaveTextButton(
              onTap: provider.onSave,
            )
          : Container(),
    );
  }

  @override
  Widget wrapperBody(Widget child) {
    return listBuilder<bool>(
        selector: (_, __) => __.isEdit == true || provider.id == null,
        builder: (BuildContext context, bool value, Widget child) =>
            FormEditStatusInheritedWidget(
              isEdit: value,
              child: child,
            ),
        child: child);
  }

  @override
  void initState() {
    provider.initData(widget.data, widget.isDuplicate);
    if (widget.id != null) provider.id = widget.id;
    super.initState();
    tabController = TabController(length: 3, vsync: this)
      ..addListener(() {
        provider.onChange(tabController);
      });
  }

  @override
  void dispose() {
    super.dispose();
    tabController.dispose();
  }

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            CommonTabBar(
              controller: tabController,
              tabs: [
                S.current.particulars,
                S.current.employment,
                S.current.others
              ]
                  .map((e) => MyTab(
                        text: e,
                        hasNeed: e != 'Others',
                      ))
                  .toList(),
            ),
            Expanded(child: _buildTabBarView()),
          ],
        ),
      );

  Widget _buildTabBarView() => TabBarView(
        controller: tabController,
        children: [
          ParticularsView(),
          EmployeeView(),
          EmployeeOtherView(),
        ],
      );
}
